package com.tianruan.dc.modules.sys.user.api;

/**
 * @Author: AmiroKD
 * @CreateTime: 2024-07-09
 * @Version: 1.0
 */

import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.sys.user.entity.dto.WordLearnDTO;
import com.tianruan.dc.modules.sys.user.service.IWordLearmService;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 每日单词
 */
@RestController
@RequestMapping("/api/wordLearn")
@Slf4j
public class WordLearnController {

    @Autowired
    private IWordLearmService iWordLearmService;

    /**
     * 分页查询单词
     * @param page
     * @param pageSize
     * @param word
     * @return
     */
    @GetMapping("/select")
    public VResponse selectWord(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam("word") String word){
        PageBean pageBean = iWordLearmService.page(page,pageSize,word);
        return VResponse.success(pageBean);
    }

    /**
     * 修改背诵状态
     * @param wordLearnDTO
     * @return
     */
    @PutMapping("/flag")
    public VResponse updateFlag(@RequestBody WordLearnDTO wordLearnDTO){
        if (wordLearnDTO.getWord().isEmpty()){
            return VResponse.error(1000,"设置单词不为空");
        }
        //判断单词是否存在
        boolean isWordExist = iWordLearmService.isWordExist(wordLearnDTO.getWord());
        if (!isWordExist){
            iWordLearmService.updateFlag(wordLearnDTO);
            return VResponse.success("成功修改背诵状态");
        }
        return VResponse.error(1001,"不存在此单词");
    }

}
